package com.example.freshmenapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

public class AddMessagesActivity extends AppCompatActivity {
    EditText etTitle;
    EditText etContent;
    Button btnTijiao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_messages);

        initView();//绑定控件
        initEvent();//设置事件
        initData();//在初始化时获取数据
    }

    private void initData() {
    }

    private void initEvent() {
        btnTijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDats();//获取用户输入的数据，访问网络，得到返回数据，更新界面
            }
        });
    }

    private void getDats() {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url = " http://10.2.64.24:8081/app8/messages/add ";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("title", etTitle.getText().toString());
            jsonObject.put("content", etContent.getText().toString());
            jsonObject.put("name","接哦");
            jsonObject.put("date","2021-09-06 00:00:00");
            //jsonObject.put("date", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));//获取电脑系统时间
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: " + jsonObject);
                /*处理结果数据*/
                // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）
                Gson gson = new Gson();
                MyBean myBean = gson.fromJson(jsonObject.toString(), MyBean.class);
                if(myBean.isSuccess()){
                    Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);

    }

    private void initView() {
        etTitle =  findViewById(R.id.et_title);//将布局中的id为r.id.et_title的控件设置为成员etTitle
        etContent = findViewById(R.id.et_content);
        btnTijiao =findViewById(R.id.btn_tijiao);
    }
}